package lex.mxbeans.utils;

import java.util.Arrays;

public class ObjectUtils {
	public static String toString(Object v) {
		if (null == v)
			return "";
		if (v.getClass().isArray())
			return arrayToString(v);
		return v.toString();
	}

	private static String arrayToString( Object obj )
	{
		Class<?> clz = obj.getClass();
		StringBuilder buf = new StringBuilder();
		if( clz.isArray() )
		{
			if( clz == byte[].class )
				buf.append( Arrays.toString( (byte[]) obj ) );
			else if( clz == short[].class )
				buf.append( Arrays.toString( (short[]) obj ) );
			else if( clz == int[].class )
				buf.append( Arrays.toString( (int[]) obj ) );
			else if( clz == long[].class )
				buf.append( Arrays.toString( (long[]) obj ) );
			else if( clz == char[].class )
				buf.append( Arrays.toString( (char[]) obj ) );
			else if( clz == float[].class )
				buf.append( Arrays.toString( (float[]) obj ) );
			else if( clz == double[].class )
				buf.append( Arrays.toString( (double[]) obj ) );
			else if( clz == boolean[].class )
				buf.append( Arrays.toString( (boolean[]) obj ) );
			else
			{ // element is an array of object references
				buf.append( Arrays.deepToString( (Object[]) obj ) );
			}
		}
		return buf.toString();
	}

	private ObjectUtils() {
	}
}
